package modelLayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * This class is part of WesternStyle sales management system. 
 * It holds information on a given sales order.
 * 
 * @author J. Korsgaard (Design)
 * 		   D. Mølgaard	(Review)
 * 		   A. Kelemen	(Test)
 * @Version 1.1
 * 			1.1	-	Changed where the exceptions are thrown. Made some general
 * 					adjustments to the syntax. (Danny) 
 */
public class SalesOrder {
	
	private int 	        orderID;
	private Date            date,
	                        deliveryDate;
	private String          deliveryStatus;
	private List<OrderLine> orderLines;
	private Customer        customer;
	private Invoice         invoice;
	
	
	/**
	 * Constructor of class salesOrder.
	 */
	public SalesOrder() {
		
		this.setDate();
		this.setDeliveryStatus("");
		this.setDeliveryDate(new Date());
		this.orderLines = new ArrayList<>();
		this.invoice = null;
		
	}
	
	/**
	 * @return the orderID
	 */
	public int getOrderID() {
		
		return this.orderID;
		
	}
	
	/**
	 * Sets the order id
	 * @param int adds orderID 
	 */
	public boolean setOrderID(int orderID) {
		
		if (orderID >= 0) {
			this.orderID = orderID;
			return true;
		}
		else {
			return false;
		}
		
	}
	
	/**
	 * @return the date
	 */
	public Date getDate() {
		
		return this.date;
		
	}
	
	/**
	 * Sets date of order
	 * @param non
	 */
	public void setDate() {
		
		this.date = new Date();
		
	}
	
	/**
	 * @return the deliveryStatus
	 */
	public String getDeliveryStatus() {
		
		return this.deliveryStatus;
		
	}
	
	/**
	 * Sets the delivery status
	 * @param  String 
	 */
	public void setDeliveryStatus(String deliveryStatus) {
		
		this.deliveryStatus = deliveryStatus;
		
	}
	
	/**
	 * @return the deliveryDate
	 */
	public Date getDeliveryDate() {
		
		return this.deliveryDate;
		
	}
	
	/**
	 * Sets delivery date of order
	 * @param Date 
	 */
	public void setDeliveryDate(Date deliveryDate) {
		
		this.deliveryDate = deliveryDate;
		
	}
	
	/**
	 * 
	 * @return the orderLines
	 */
	public List<OrderLine> getOrderLines() {
		
		List<OrderLine> ol = new ArrayList<>();
		if (this.orderLines.size() > 0) {
			ol = this.orderLines;
		}
		return ol;
		
	}

	
	/**
	 * Adds orderLines to the list
	 * @param orderLine
	 * @return true if orderLine added successfully
	 */
	public boolean addOrderLine(OrderLine orderLine) {
		
		boolean result = false;
		if (orderLine != null && !this.orderLines.contains(orderLine)) {
			result = this.orderLines.add(orderLine);
			if (orderLine.getSalesOrder() == null) {
				orderLine.setSalesOrder(this);
			}
		}
		return result;
	}
	
	/**
	 * Removes an orderLine from the collection.
	 * @param ol
	 * @return true if orderLine removed successfully
	 */
	public boolean removeOrderLine(int productID) {
		
		boolean found = false;
		int		i = 0;
		
		while (!found && i < this.orderLines.size()) {
			if (this.orderLines.get(i).getProduct().getProductID() == productID) {
				this.orderLines.remove(i);
				found = true;
			}
			else {
				++i;
			}
		}
		return found;
		
	}

	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		
		return this.customer;
		
	}

	/**
	 * sets the customer on an order
	 * @param customer 
	 */
	public void setCustomer(Customer customer) {
		
		this.customer = customer;
		
	}

	/**
	 * @return the invoice
	 */
	public Invoice getInvoice() {
		
		if (this.invoice != null) {
			
			return this.invoice;
		}
		else {
			return null;
		}
	}

	/**
	 * sets the invoice of the order
	 * @param invoice 
	 */
	public void setInvoice(Invoice invoice) {
		
		this.invoice = invoice;
		
	}
	
}
